import time

from re_common.facade.use.mq_use_facade import UseMq


def send_test1():
    """
    该测试样例使用最简单的自动确认和循环调度原则(每个客户端取一个，保证客户端取消息个数一致)
    实际使用中没有太大意义，因为不可能不停的取什么都不做
    :return:
    """
    from re_common.baselibrary.utils.basepika import BasePika

    basepika = BasePika()
    basepika.set_default()
    basepika.connect_str('amqp://vipcj:cqcoll123@192.168.31.123:5672/airflow')
    # basepika.connect()
    basepika.create_channel()
    basepika.queue_declare()
    while True:
        print("send msg")
        basepika.easy_send_msg()
        time.sleep(3)


# send_test1()

def send_test2():
    """
    不使用自动确认,加入持久化和设置一次分发任务个数
    :return:
    """
    from re_common.baselibrary.utils.basepika import BasePika

    basepika = BasePika()
    basepika.set_default()
    basepika.connect()
    basepika.create_channel()
    basepika.queue_declare(queue="task_queue2", durable=True)
    properties = basepika.get_properties()
    while True:
        if basepika.get_queue_size("task_queue2") < 100:
            print("send msg")
            basepika.easy_send_msg(routing_key="task_queue2",body="task_queue2", properties=properties)
        else:
            time.sleep(3)


# send_test2()

def send_test3():
    """
    使用 fanout 该exchange会将消息发送到绑定的所有队列，加入临时队列
    :return:
    """
    from re_common.baselibrary.utils.basepika import BasePika

    basepika = BasePika()
    basepika.set_default()
    basepika.connect()
    basepika.create_channel()
    basepika.exchange_declare()
    # queuename = basepika.random_queue_declare().method.queue
    # 如果不绑定队列 消息将被忽略
    # basepika.queue_bind(queue=queuename)
    properties = basepika.get_properties()
    i = 0
    while True:
        i = i + 1
        print("send msg {}".format(i))
        # 当我们指定exchange为fanout exchange时，这个值(routing_key)将忽略。
        basepika.easy_send_msg(exchange="logs", routing_key="", body=str(i), properties=properties)
        time.sleep(1)


# send_test3()

def send_test4():
    """
    使用 fanout 该exchange会将消息发送到绑定的所有队列，加入临时队列
    :return:
    """
    from re_common.baselibrary.utils.basepika import BasePika

    basepika = BasePika()
    basepika.set_default()
    basepika.connect()
    basepika.create_channel()
    basepika.exchange_declare(exchangename="direct_logs", type="direct")
    properties = basepika.get_properties()
    i = 0
    while True:
        key = i % 3
        print(key)
        i = i + 1
        print("send msg {}".format(i))
        # routing_key决定将消息发送到哪个队列
        basepika.easy_send_msg(exchange="direct_logs", routing_key=str(key), body=str(i), properties=properties)
        time.sleep(1)


# send_test4()


def send_test5():
    """
    使用 fanout 该exchange会将消息发送到绑定的所有队列，加入临时队列
    :return:
    """
    from re_common.baselibrary.utils.basepika import BasePika

    basepika = BasePika()
    basepika.set_default()
    basepika.connect()
    basepika.create_channel()
    basepika.exchange_declare(exchangename="topic_logs", type="topic")
    properties = basepika.get_properties()
    i = 0
    while True:
        i = i + 1
        for key in ["a.b", "a.b.c", "a.b.c.d"]:
            print("send msg {}".format(i))
            # routing_key决定将消息发送到哪个队列
            basepika.easy_send_msg(exchange="topic_logs", routing_key=key, body=key + str(i), properties=properties)
            time.sleep(1)


# send_test5()


def send_test6():
    use = UseMq("test.reconn.test")
    for i in range(10000):
        while True:
            time.sleep(10)
            if use.send_mq(str(i), num=100):
                print(i)
                break

send_test6()